package org.cn.osiris.org.controller;

import org.cn.osiris.basic.exception.BusinessException;
import org.cn.osiris.basic.util.ExcelUtils;
import org.cn.osiris.org.domain.ShopAuditLog;
import org.cn.osiris.org.domain.ShopDto;
import org.cn.osiris.org.service.IShopService;
import org.cn.osiris.org.domain.Shop;
import org.cn.osiris.org.query.ShopQuery;
import org.cn.osiris.basic.util.AjaxResult;
import org.cn.osiris.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 后端接口类；
 * @author DELL
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null) {
                shopService.update(shop);
            } else {
                shopService.add(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
                shopService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id")Long id) {
        return shopService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }


    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 审核接口
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try{
            shopService.pass(shopAuditLog);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult();
        }
    }

    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 导出接口
     */
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.findAll();
            //exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,
            //              导出的数据           标题          表名              导出类型        文件名
            ExcelUtils.exportExcel(shops, "店铺列表", "入驻店铺数据", Shop.class, "入驻店铺数据.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入接口
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestBody MultipartFile file){
        //(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)
        //      上传的文件               忽略标题行           忽略表头行               传输文件生成类型
        List<Shop> shops = ExcelUtils.importExcel(file, 1, 1, Shop.class);
        shops.forEach(System.out::println);
        for (Shop s: shops) {
            shopService.add(s);
        }
    }



    /**
     * 激活接口
     */
    @GetMapping("/active/{id}")
    public void activation(@PathVariable("id") long id){
        System.out.println("进入了active接口");
        shopService.active(id);

//        System.out.println("结束了");
//        //查询到所处那过来的id对应的对象
//        Shop shop = shopService.findById(id);
//
//        //修改所查找到对象的状态值字段
//        shop.setState(3);
//
//        //修改对象
//        shopService.update(shop);
//
//        //保存审核日志
//        shopAuditLog.setState(3);
//        shopService.add(shopAuditLog);
//
//
//        //弹窗提示 JOptionPane.showMessageDialog(null, “警告”,”提示”,JOptionPane.WARNING_MESSAGE);
//        JOptionPane.showMessageDialog(null,"你已成功激活改店铺","提示",JOptionPane.WARNING_MESSAGE);

    }

    @GetMapping("/echarts")
    public List<ShopDto> echartsData(){
        List<ShopDto> echarts = shopService.echarts();
        echarts.forEach(System.out::println);

        return echarts;
    }
}
